package com.report.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.zhxu.bs.BeanSearcher;
import cn.zhxu.bs.SearchResult;
import cn.zhxu.bs.operator.Equal;
import cn.zhxu.bs.util.MapUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.report.dto.request.DownloadReportReq;
import com.report.dto.response.DownloadReportRes;
import com.report.dto.response.MemberRes;
import com.report.entity.DownloadReport;
import com.report.entity.Rule;
import com.report.mapper.DownloadReportMapper;
import com.report.service.DownloadReportService;
import com.report.service.IMemberRuleService;
import com.report.service.IMemberService;
import com.zmh.framework.constants.Constants;
import com.zmh.framework.exception.ResultException;
import com.zmh.framework.sys.dto.request.common.PageResponseDTO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * @author summer
 * @version 1.0
 * @date 2024/7/31 09:05
 */
@Service
@Transactional(rollbackFor = Exception.class)
@AllArgsConstructor
public class DownloadReportServiceImpl extends ServiceImpl<DownloadReportMapper, DownloadReport> implements DownloadReportService {

    private final BeanSearcher beanSearcher;



    @Override
    public Page<DownloadReportRes> lists(PageResponseDTO pageResponseDTO) {
        Object loginId = StpUtil.getLoginId();
        Page<DownloadReportRes> page=new Page<>(pageResponseDTO.getPageNum(),pageResponseDTO.getPageSize());
        Map<String, Object> build = MapUtils.builder()
                .page(pageResponseDTO.getPageNum()-1,pageResponseDTO.getPageSize())
                .field(DownloadReportRes::getMemberId, loginId).op(Equal.class).build();
        SearchResult<DownloadReportRes> search = beanSearcher.search(DownloadReportRes.class, build);
        page.setTotal(search.getTotalCount().longValue());
        page.setRecords(search.getDataList());
        return page;
    }

    @Override
    public Page<DownloadReportRes> downloadReportLists(DownloadReportReq pageResponseDTO) {

        Page<DownloadReportRes> page=new Page<>(pageResponseDTO.getPageNum(),pageResponseDTO.getPageSize());
        Map<String, Object> build = MapUtils.builder()
                .page(pageResponseDTO.getPageNum()-1,pageResponseDTO.getPageSize())
                .field(DownloadReportRes::getMemberId, pageResponseDTO.getMemberId()).op(Equal.class).build();
        SearchResult<DownloadReportRes> search = beanSearcher.search(DownloadReportRes.class, build);
        page.setTotal(search.getTotalCount().longValue());
        page.setRecords(search.getDataList());
        return page;
    }

    private final IMemberService memberService;



    @Override
    public void findMemberDownloadReportFile() {
        downloadDay(StpUtil.getLoginIdAsInt());
    }

    @Override
    public void downloadDay(Integer memberId) {
        MemberRes memberRes = memberService.getByMemberInfo(memberId);
        //当日下载数量
        Map<String, Object> build = MapUtils.builder().field(DownloadReportRes::getMemberId,memberId).op(Equal.class)
                .field(DownloadReportRes::getCreateTime).sql("DATE($1) =CURDATE()").build();
        Number number = beanSearcher.searchCount(DownloadReportRes.class, build);
        int downloadDay = number.intValue();

        //当月下载量
        Map<String, Object> built = MapUtils.builder().field(DownloadReportRes::getMemberId, memberId).op(Equal.class)
                .field(DownloadReportRes::getCreateTime).sql("MONTH($1) = MONTH(CURDATE())").build();
        Number number1 = beanSearcher.searchCount(DownloadReportRes.class, built);
        int downloadMonth = number1.intValue();


        if(ObjectUtil.isNotEmpty(memberRes)){
            Integer downloadDay2 = memberRes.getDownloadDay();
            Integer downloadMonth2 = memberRes.getDownloadMonth();

            if(downloadDay>=downloadDay2){
                throw new ResultException("今日权限已用完，可以升级权益");
            }
            if(downloadMonth>=downloadMonth2){
                throw new ResultException("当月权限已用完，可以升级权益");
            }
        }else{
            throw new ResultException("会员信息查询失败,请联系客服");
        }
    }

    @Override
    public void saveDownloadData(DownloadReportReq downloadReportReq) {
         LambdaQueryWrapper<DownloadReport> downloadReportLambdaQueryWrapper = Wrappers.lambdaQuery(DownloadReport.class).eq(DownloadReport::getReportId, downloadReportReq.getReportId()).eq(DownloadReport::getMemberId, downloadReportReq.getMemberId()).orderByAsc(DownloadReport::getCreateTime);

        DownloadReport one = this.getOne(downloadReportLambdaQueryWrapper);
        if(one==null){
            one = new DownloadReport();
            one.setMemberId(downloadReportReq.getMemberId());
            one.setReportId(downloadReportReq.getReportId());
            this.save(one);
        }

    }
}
